﻿Imports System.Math
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO
Imports System.Drawing.Imaging


Public Class Form5

    Dim xMask(,) As Single = New Single(,) {{-1, 0, 1}, _
                                               {-2, 0, 2}, _
                                               {-1, 0, 1}}

    Dim yMask(,) As Single = New Single(,) {{1, 2, 1}, _
                                            {0, 0, 0}, _
                                            {-1, -2, -1}}

    Private Sub btnBrowseImage_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowseImage.Click
        ofdLoadImage.Title = "Please Select a File"
        ofdLoadImage.InitialDirectory = "C:temp"
        ofdLoadImage.ShowDialog()

        Try
           
            If ofdLoadImage.FileName = "" Then Return

            If Not Me.PictureBox1.Image Is Nothing Then
                Me.PictureBox1.Image.Dispose()
            End If

            Me.PictureBox1.Image = Image.FromFile(ofdLoadImage.FileName) 'New Bitmap(ofdLoadImage.FileName)
        Catch ex As Exception
            MsgBox("Error: " & ex.Message)
        End Try
    End Sub

    Private Sub btnScanImage_Click(sender As System.Object, e As System.EventArgs) Handles btnScanImage.Click
        SafeDispose(CType(Me.PictureBox2.Image, IDisposable))
        Me.PictureBox2.Image = Nothing
        Me.PictureBox2.Refresh()
        'Dim t As New VisualCore.Utilities.PrecisionTimer()
        't.Start()
        Me.PictureBox2.Image = DetectEdges(CType(Me.PictureBox1.Image, Bitmap))
        'ShowPixelSpeed(PictureBox2.Image.Width * PictureBox2.Image.Height, t.GetElapsedTime)
        Me.PictureBox2.Refresh()
    End Sub

    Sub SafeDispose(ByRef d As IDisposable)
        If Not d Is Nothing Then
            d.Dispose()
        End If
    End Sub
    Function DetectEdges(ByVal inImg As Bitmap) As Bitmap
        Dim out As New Bitmap(inImg.Width, inImg.Height, Imaging.PixelFormat.Format32bppArgb)

        For y As Integer = 0 To inImg.Height - 1
            For x As Integer = 0 To inImg.Width - 1
                Dim gradX As Single = 0
                Dim gradY As Single = 0
                Dim grad As Single = 0

                If x = 0 Or y = 0 Or x = inImg.Width - 1 Or y = inImg.Height - 1 Then
                    grad = 0
                Else
                    For i As Integer = -1 To 1
                        For j As Integer = -1 To 1
                            Dim p As Color = inImg.GetPixel(x + i, y + j)
                            Dim intensity As Single = 0.333F * (CInt(p.R) + p.G + p.B)
                            '// approximate X gradient
                            gradX += intensity * xMask(i + 1, j + 1)
                            '// approximate Y gradient
                            gradY += intensity * yMask(i + 1, j + 1)
                        Next
                    Next

                    grad = (Math.Abs(gradX) + Math.Abs(gradY))
                    '   grad = grad / 100
                End If

                grad = Math.Max(0, grad)
                '// could easily add a threshold here:
                'If grad < 50 Then
                '    grad = 0
                'End If

                grad = Math.Min(255, grad)

                'grad = 255 - grad

                out.SetPixel(x, y, Color.FromArgb(CInt(grad), CInt(grad), CInt(grad)))
            Next

            Me.PictureBox2.Image = out
            Me.PictureBox2.Refresh()
        Next

        Return out
    End Function

   
End Class